﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using DungChung;
using System.IO;
namespace CoVuaClient
{
    /// <summary>
    /// Interaction logic for DangKi.xaml
    /// </summary>
    public partial class DangKi : Window
    {
        
        public DangKi()
        {
            InitializeComponent();
        }
        public string ipServer;
        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void buttonDangKi_Click(object sender, RoutedEventArgs e)
        {
            if (passwordPass.Password == passwordRePass.Password &&
                textUserName.Text != string.Empty &&
                textHoTen.Text!= string.Empty &&
                passwordPass.Password != string.Empty)
            {
                DangKiTask();
            }
            else
            {
                if (passwordPass.Password != passwordRePass.Password)
                {
                    MessageBox.Show("2 password không trùng khớp");
                }
                else
                {
                    MessageBox.Show("Bạn chưa nhập đủ thông tin!\nVui lòng nhập thêm!");
                }
                
            }
            
        }

        private void DangKiTask()
        {
            try
            {
                TcpClient tcpClient = new TcpClient();
                tcpClient.Connect(IPAddress.Parse(ipServer), 8000);
                NetworkStream networkStream = tcpClient.GetStream();
                BinaryReader reader = new BinaryReader(networkStream);
                BinaryWriter writer = new BinaryWriter(networkStream);
                writer.Write(MyTask.DangKi);

                writer.Write(textUserName.Text);
                writer.Write(textHoTen.Text);
                writer.Write(textGioiThieu.Text);
                writer.Write(passwordPass.Password);

                if (radioNam.IsChecked.Value)
                {
                    writer.Write("Nam");
                }
                else
                {
                    writer.Write("Nữ");
                }
                writer.Write(dateNgaySinh.Text);
                string ketQua = reader.ReadString();
                if (ketQua == "OK")
                {
                    MessageBox.Show("Đăng kí thành công!");
                }
                else
                {
                    MessageBox.Show("Đăng kí thất bại.\nVui lòng thử lại!");
                }
                tcpClient.Close();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        private void textHoTen_TextChanged(object sender, TextChangedEventArgs e)
        {
        }
    }
}
